from datetime import datetime
from django.contrib.auth.models import User
from django.db.models import *
from django.utils.safestring import mark_safe
from pyplog.blog.utils import convert_markup, markup_choices
from pyplog.image_processors import get_image_obj, resize_to_fit, square_bw_thumbnail

class Profession(Model):
	title = CharField(max_length = 32, unique = True)
	
	def __unicode__(self):
		return self.title
	
	@classmethod
	def add_defaults(cls, sender, **kwars):
		"""
		Adds a useful set of professions.
		"""
		defaults = (
			"Photographer",
			"Model",
			"Make-up artist",
			"Hair stylist",
			"Fashion designer",
			"Retouching artist",
		)
		print "Adding default professions..."
		for p in defaults:
			try:
				cls.objects.create(title = p)
			except:
				continue
module = __import__("pyplog.artists.models", fromlist = [None], level = 0)
signals.post_syncdb.connect(Profession.add_defaults, sender = module)

class Artist(Model):
	first_name = CharField(max_length = 32)
	last_name = CharField(max_length = 32)
	profession = ForeignKey(Profession, related_name = "artists")
	slug = SlugField(unique = True)
	included_in_index = BooleanField(default = False)
	website = URLField(blank = True)
	email = EmailField(blank = True)
	bio = TextField(blank = True)
	bio_markup = CharField("Markup", max_length = 3, default = 1, choices = markup_choices)
	bio_html = TextField(blank = True)
	portrait_source_image = ImageField("Portrait", upload_to = "img/artists/portrait-source", blank = True, null = True, help_text = "The image should be larger than 150x150")
	
	def __unicode__(self):
		return self.name + " (" + str(self.profession) + ")"
	
	@property
	def samples(self):
		return self.photos.all()[:5]
	
	@permalink
	def get_absolute_url(self):
		return ("artist-detail", (), { "slug" : self.slug })
	
	@permalink
	def get_feed_url(self):
		return ("photo-feed", (), { "url" : "artist/" + self.slug })
	
	@permalink
	def get_blog_feed_url(self):
		return ("post-feed", (), { "url" : "author/" + self.slug })
	
	@property
	def detail_image(self):
		return "img/artists/portrait-images/" + self.slug + ".jpg"
	
	@property
	def thumbnail_image(self):
		return "img/artists/portrait-thumbnails/" + self.slug + ".jpg"
	
	@property
	def photoset_description(self):
		return mark_safe("Photos in the creation of which <a href='" + self.get_absolute_url() + "'>" + self.name + "</a> was involved")
	
	@property
	def postset_description(self):
		return mark_safe("Blog posts written by <a href='" + self.get_absolute_url() + "'>" + self.name + "</a>")
	
	@property
	def name(self):
		return self.first_name + " " + self.last_name
	
	@property
	def first_name_genitive(self):
		return self.first_name + "'s"
	
	@property
	def title(self):
		"""
		Alias needed for this model to be used as a photo key.
		"""
		return self.name
	
	def save(self):
		self.bio_html = convert_markup(self.bio, self.bio_markup)
		if self.portrait_source_image:
			source_image = get_image_obj(self.portrait_source_image)
			detail_image = resize_to_fit(source_image.copy(), (150, 400))
			thumbnail_image = square_bw_thumbnail(detail_image.copy())
			detail_image.save(settings.MEDIA_ROOT + self.detail_image, quality = 90)
			thumbnail_image.save(settings.MEDIA_ROOT + self.thumbnail_image, quality = 80)
		super(Artist, self).save()
